package socket;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author LuXingda
 * @version 1.0
 * @data 2024/8/20 10:24
 */
//客户端
public class Client_ extends JFrame implements ActionListener {
    private Socket socket;
    private PrintWriter writer;
    private BufferedReader reader;
    private JTextArea chatArea;
    private JTextField inputField;
    private JButton sendButton;

    public Client_() {
        super("Chat Client");

        try {
            System.out.println("正在启动中.....");
            socket = new Socket("localhost", 8088);
            System.out.println("服务器连接成功");

            // 获取输出流，用于向服务器发送消息
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            writer = new PrintWriter(bw, true); // 自动刷新

            // 获取输入流，用于从服务器接收消息
            InputStream in = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
            reader = new BufferedReader(isr);

            // 设置布局
            setLayout(new BorderLayout());

            // 文本区域用于显示聊天记录
            chatArea = new JTextArea(20, 50);
            chatArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(chatArea);
            add(scrollPane, BorderLayout.CENTER);

            // 输入框和发送按钮
            JPanel inputPanel = new JPanel();
            inputField = new JTextField(40);
            sendButton = new JButton("Send");
            sendButton.addActionListener(this);
            inputPanel.add(inputField);
            inputPanel.add(sendButton);
            add(inputPanel, BorderLayout.SOUTH);

            // 启动用于读取服务端消息的线程
            Thread readThread = new Thread(() -> {
                try {
                    // 循环读取来自服务器发送过来的消息并显示在文本区域
                    String message;
                    while ((message = reader.readLine()) != null) {
                        chatArea.append(message + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            readThread.start();

            // 设置窗口大小和关闭行为
            setSize(600, 400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sendButton) {
            String message = inputField.getText();
            if (!message.trim().isEmpty()) {
                writer.println(message); // 发送消息给服务器
                chatArea.append("You: " + message + "\n"); // 显示自己发送的消息
                inputField.setText(""); // 清空输入框
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Client_ client = new Client_();
            client.start();
        });
    }

    public void start() {
        // 这里不需要显式调用start方法，因为构造函数中已经完成了初始化
    }
}
